/**
 * @file subscription.cpp
 * @brief Subscription 类实现，负责 RAII 式退订。
 */

#include "subscription.h"

#include "event_bus.h"  // 需要 EventBus::Unsubscribe 的定义。

Subscription::Subscription(EventBus* bus, std::string topic, std::size_t id)
    : bus_(bus), topic_(std::move(topic)), id_(id) {}

Subscription::Subscription(Subscription&& other) noexcept {
    *this = std::move(other);
}

Subscription& Subscription::operator=(Subscription&& other) noexcept {
    if (this != &other) {
        Reset();  // 先退订当前持有的。
        bus_ = other.bus_;
        topic_ = std::move(other.topic_);
        id_ = other.id_;
        other.bus_ = nullptr;
        other.id_ = 0;
    }
    return *this;
}

Subscription::~Subscription() {
    Reset();
}

void Subscription::Reset() {
    if (bus_) {
        bus_->Unsubscribe(topic_, id_);
        bus_ = nullptr;  // 防止重复退订。
    }
}